package com.xauto.pioneer.appuser.activity.common;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.WebInfo;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;

/**
 * webView
 * Created by Lemon on 16/5/13.
 */
public class WebViewActivity extends BaseActivity {
    private static String REQUEST_TYPE = "001";
    private static String REQUEST_HAVE_LOAD = "002";
    private static String REQUEST_LOAD_URL = "003";
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.webView)
    WebView webView;
    @Bind(R.id.consume_money_text)
    TextView consumeMoneyText;
    @Bind(R.id.start_level_text)
    TextView startLevelText;
    @Bind(R.id.progressBar)
    ProgressBar progressBar;
    @Bind(R.id.end_level_text)
    TextView endLevelText;
    @Bind(R.id.vip_linear)
    LinearLayout vipLinear;
    @Bind(R.id.tvWebView)
    TextView tvWebView;
    @Bind(R.id.percent_text)
    TextView percentText;
    //1-费用预估-电车  2-费用预估-油车 3-分时租赁协议 4-VIP等级说明 5-关于我们 6-用车须知 7-订单明细 8-费用预估
    private int requestType;
    private boolean isRequestLoadUrl = false;

    /**
     * 入口
     * @param mContext
     * @param type 1-费用预估-电车  2-费用预估-油车 3-分时租赁协议 4-VIP等级说明 5-关于我们 6-用车须知
     */
    public static void startAction(Context mContext, int type) {
        Intent intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra(REQUEST_TYPE, type);
        mContext.startActivity(intent);
    }

    /**
     * 入口
     * @param mContext
     * @param isRequestLoadUrl
     */
    public static void startAction(Context mContext,boolean isRequestLoadUrl,String url) {
        Intent intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra(REQUEST_HAVE_LOAD, isRequestLoadUrl);
        intent.putExtra(REQUEST_LOAD_URL, url);
        mContext.startActivity(intent);
    }
    @Override
    protected int getContentView() {
        return R.layout.ac_webview;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        requestType = getIntent().getIntExtra(REQUEST_TYPE, 1);
        isRequestLoadUrl = getIntent().getBooleanExtra(REQUEST_HAVE_LOAD, false);
        WebSettings webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                if (isRequestLoadUrl){
                    topBar.setText(TopBar.TITLE, title);
                }
                super.onReceivedTitle(view, title);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isRequestLoadUrl){
            topBar.setText(TopBar.TITLE, "");
            webView.loadUrl(getIntent().getStringExtra(REQUEST_LOAD_URL));
        }else {
            loadData();
        }
    }

    /**
     * 加载网页信息
     */
    private void loadData() {
        new LHttpLib().getWebInfo(mContext, String.valueOf(requestType),memberId,"","", new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final WebInfo webInfo = new WebInfo().parse(jsonStatus.data);
                if (webInfo == null) return;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        topBar.setText(TopBar.TITLE, webInfo.data_value.title);
                        if ("1".equals(webInfo.data_type)) {
                            webView.loadUrl(webInfo.data_value.content);
                        } else {
                            webView.loadData(webInfo.data_value.content, "text/html; charset=UTF-8", null);//这种写法可以正确解码
                        }
                    }
                });

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

}
